<?php

namespace app\common\model\user;

use app\common\model\goods\Category;
use app\common\model\goods\category\Group;
use app\common\model\order\Goods;
use think\Model;
use app\common\model\order\Order;
use app\common\model\user\Group as UserGroup;

class Info extends Model
{





    // 表名
    protected $name = 'user_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'expirationtime_text',
        'createtime_text',
        'updatetime_text',
        'content_array',
        'my_amount_spent',
        'end_day',
        'next_level',
        'images_array',
    ];
    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            if (isset($changed['accredit_scale'])) {
                if ($changed['accredit_scale'] <= 0.01) {
                    $row->accredit = 0;
                } else {
                    $row->accredit = 1;
                }
            }
            if (isset($changed['shareholder'])) {
                $row->edittime = time();
            }
            if (isset($changed['investment'])) {
                $row->altertime = time();
            }
        });
    }
    public function getNextLevelAttr($v, $d)
    {
        if (in_array($d['group_id'], [1, 2])) {
            return UserGroup::get($d['group_id'] + 1);
        }
    }

    public function getImagesArrayAttr($v, $d)
    {
        if ($d['images']) {
            $array = explode(',', $d['images']);
            foreach ($array as &$value) {
                $value = $value ? cdnurl($value, true) : '';
            }
            return $array;
        }
        return [];
    }
    public function getMyAmountSpentAttr($v, $d)
    {
        return Order::where('order_status', '>', '0')->where('user_id', $d['user_id'])->sum('pay_fee');
    }
    public function getEndDayAttr($v, $d)
    {
        $day1 = time(); //2020-4-26 23:59:59
        $day2 = $d['expirationtime']; //2020-4-27 0:00:00
        if ($day2) {
            //date_diff两个参数 day1 day2互换位置也可以 不影响结果
            $cha = date_diff(date_create(date('Ymd', $day1)), date_create(date('Ymd', $day2)));
            return $cha->days;
        }
        return 0;
    }
    public function getContentArrayAttr($value, $data)
    {
        return $data['content'] ? json_decode($data['content'], true) : [];
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatetime']) ? $data['updatetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getUpdatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatetime']) ? $data['updatetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getExpirationtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expirationtime']) ? $data['expirationtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setExpirationtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setSettlementtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value . " 23:59:59") : $value);
    }
    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function firstcate()
    {
        return $this->belongsTo(Category::class, 'first_cate', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function twocate()
    {
        return $this->belongsTo(Category::class, 'two_cate', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function upgradeinfo()
    {
        return $this->belongsTo('Group', 'shareholder', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function investmentinfo()
    {
        return $this->belongsTo('Group', 'investment', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function group()
    {
        return $this->belongsTo(UserGroup::class, 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
